package com.coolgor.coldot.service.auth.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.coolgor.coldot.dao.auth.LoginInfoDao;
import com.coolgor.coldot.dto.auth.LoginInfoExecution;
import com.coolgor.coldot.entity.auth.LoginInfo;
import com.coolgor.coldot.enums.auth.LoginStateEnum;
import com.coolgor.coldot.exceptions.auth.LoginInfoOperationException;
import com.coolgor.coldot.service.auth.LoginInfoService;
import com.coolgor.coldot.common.PageCalculator;



@Service
public class LoginInfoServiceImpl implements LoginInfoService {
	private static Logger log = LoggerFactory.getLogger(LoginInfoServiceImpl.class);
	
	@Autowired
	private LoginInfoDao loginInfoDao;

	@Override
	public LoginInfoExecution getLoginInfoList(LoginInfo loginInfoCondition, int pageIndex, int pageSize) {
		//将页码转换成行码
		int rowIndex = PageCalculator.calculateRowIndex(pageIndex, pageSize);
		//依据查询条件，调用dao层返回相关的查询结果列表
		List<LoginInfo> loginInfoList = loginInfoDao.queryLoginInfoList(loginInfoCondition, rowIndex, pageSize);
		//依据相同的查询条件，返回查询结果总数
		int count = loginInfoDao.queryLoginInfoCount(loginInfoCondition);
		LoginInfoExecution result = new LoginInfoExecution();
		if (loginInfoList != null) {
			result.setState(LoginStateEnum.SUCCESS.getState());
			result.setStateInfo(LoginStateEnum.SUCCESS.getStateInfo());
			result.setLoginInfoList(loginInfoList);
			result.setCount(count);
		} else {
			result.setState(LoginStateEnum.INNER_ERROR.getState());
		}
		return result;
	}

	@Override
	@Transactional
	public LoginInfoExecution addLoginInfo(LoginInfo loginInfo) throws LoginInfoOperationException {
		// 空值判断
		if (loginInfo == null) {
			return new LoginInfoExecution(LoginStateEnum.INNER_ERROR);
		}
		int effectedNum;
		try {
			// 赋初始值
			loginInfo.setLoginTime(new Date());
			// 添加信息
			effectedNum = loginInfoDao.insertLoginInfo(loginInfo);
			if (effectedNum <= 0) {
				throw new LoginInfoOperationException("用户登录信息记录失败");
			} 
		} catch (Exception e) {
			throw new LoginInfoOperationException("Add LoginInfo error:" + e.getMessage());
		}
		return new LoginInfoExecution(LoginStateEnum.SUCCESS, loginInfo);
	}

	@Override
	@Transactional
	public LoginInfoExecution modifyLoginInfo(LoginInfo loginInfo) throws LoginInfoOperationException {
		// 空值判断
		if (loginInfo == null) {
			return new LoginInfoExecution(LoginStateEnum.INNER_ERROR);
		}
		int effectedNum;
		try {
			// 更新操作
			effectedNum = loginInfoDao.updateLoginInfo(loginInfo);
			if (effectedNum <= 0) {
				throw new LoginInfoOperationException("用户登录信息更新失败");
			} 
		} catch (Exception e) {
			throw new LoginInfoOperationException("Modify LoginInfo error:" + e.getMessage());
		}
		return new LoginInfoExecution(LoginStateEnum.SUCCESS, loginInfo);
	}

	@Override
	@Transactional
	public LoginInfoExecution removeLoginInfo(LoginInfo loginInfo) throws LoginInfoOperationException {
		// 空值判断
		if (loginInfo == null) {
			return new LoginInfoExecution(LoginStateEnum.INNER_ERROR);
		}
		int effectedNum;
		try {
			// 执行删除操作
			effectedNum = loginInfoDao.deleteLoginInfo(loginInfo);
			if (effectedNum <= 0) {
				throw new LoginInfoOperationException("用户登录信息删除失败");
			} 
		} catch (Exception e) {
			throw new LoginInfoOperationException("Delete LoginInfo error:" + e.getMessage());
		}
		return new LoginInfoExecution(LoginStateEnum.SUCCESS, loginInfo);
	}

	@Override
	@Transactional
	public LoginInfoExecution batchRemoveLoginInfo(List<LoginInfo> loginInfoList) throws LoginInfoOperationException {
		// 空值判断
		if (loginInfoList == null) {
			return new LoginInfoExecution(LoginStateEnum.INNER_ERROR);
		}
		List<Long> recordIDList = new ArrayList<Long>();
		for (int i=0; i<loginInfoList.size(); i++) {
			recordIDList.add(loginInfoList.get(i).getRecordID());			
		}
		int effectedNum;
		try {
			// 执行删除操作
			effectedNum = loginInfoDao.batchDeleteLoginInfo(recordIDList);
			if (effectedNum <= 0) {
				throw new LoginInfoOperationException("用户登录信息批量删除失败");
			} 
		} catch (Exception e) {
			throw new LoginInfoOperationException("Batch Delete LocalUser error:" + e.getMessage());
		}
		LoginInfoExecution exe = new LoginInfoExecution(LoginStateEnum.SUCCESS);
		//将删除的记录总数加入到返回值
		exe.setCount(effectedNum);
		return exe;
	}

}
